package com.fengye.algorithms.leecode;

/**
 * @Author fengye
 * @Date 2023/4/10 9:47
 **/
public class NearestValidPoint1779 {
    public int nearestValidPoint(int x, int y, int[][] points) {
        int min = Integer.MAX_VALUE;
        int start = Integer.MAX_VALUE;
        for(int i=0;i< points.length;i++) {
            if(points[i][0] == x || points[i][1] == y) {
                int t=Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y);
                if(min >= t) {
                    if(min > t) {
                        start = i;
                    } else {
                        start = Math.min(start,i);
                    }
                    min = Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y);
                }

            }
        }
        return start == Integer.MAX_VALUE ? -1 : start;
    }
}
